package com.ji.server;

import com.ji.protocol.JCProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.ByteBuffer;

/**
 * Title:
 * Description:
 * Author:吉
 * Since:2017/5/19
 * Version:1.1.0
 */
public class ProtocolEncoder extends MessageToByteEncoder<JCProtocol> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, JCProtocol jcProtocol, ByteBuf byteBuf) throws Exception {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(jcProtocol.getContent().getBytes().length + 4);
        byteBuf.writeBytes(buffer.array());
        byteBuf.writeBytes(jcProtocol.getContent().getBytes());
    }
}
